package com.yoomiito.app.adapter.home.reommend

import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.LayoutHelper
import com.yoomiito.app.R
import com.yoomiito.app.model.ViewType
import com.yoomiito.app.model.home.HomeVideo
import com.yoomiito.app.model.home.VideoClipInfo
import com.yoomiito.app.ui.video.clip.VideoClipListActivity
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.widget.SpacingDecoration

class HomeVideoClipAdapter(val context: Context,
                           val layoutHelper:LayoutHelper,
                           val data:List<HomeVideo>,
                           val viewPool: androidx.recyclerview.widget.RecyclerView.RecycledViewPool)
    : DelegateAdapter.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>(){

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_home_video_clip, p0, false)
        return ItemViewHolder(view)
    }

    override fun getItemCount(): Int = data.size

    override fun onCreateLayoutHelper(): LayoutHelper = layoutHelper

    override fun onBindViewHolder(p0: androidx.recyclerview.widget.RecyclerView.ViewHolder, p1: Int) {
        val holder = p0 as ItemViewHolder
        val homeVideo = data[p1]
        holder.rcy.setRecycledViewPool(viewPool)
        val layoutManager = androidx.recyclerview.widget.LinearLayoutManager(context)
        layoutManager.orientation = androidx.recyclerview.widget.LinearLayoutManager.HORIZONTAL
        holder.rcy.layoutManager = layoutManager
        val data = homeVideo.data?.filter { it.viewType!=22 }?.map { it.t } as ArrayList<VideoClipInfo>
        val adapter = HomeItemVideoClipAdapter(context,homeVideo.data,data)
        holder.rcy.adapter = adapter
        holder.videoMore.setOnClickListener { VideoClipListActivity.start(context,10,data)  }

    }

    private inner class ItemViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView) {
        val videoMore:LinearLayout = itemView.findViewById(R.id.videoClipMore)
        val rcy: androidx.recyclerview.widget.RecyclerView = itemView.findViewById(R.id.rcy)
    }
}